package com.sixteen.ordering_system.controller;


import com.sixteen.ordering_system.mapper.DishCategoryMapper;
import com.sixteen.ordering_system.mapper.DishMapper;
import com.sixteen.ordering_system.mapper.SysOrderMapper;
import com.sixteen.ordering_system.mapper.SysUserMapper;
import com.sixteen.ordering_system.pojo.Dish;
import com.sixteen.ordering_system.pojo.DishCategory;
import com.sixteen.ordering_system.pojo.SysOrder;
import com.sixteen.ordering_system.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("bus")
public class BusController {
    @Autowired
    private SysOrderMapper sysOrderMapper;
    @Autowired
    private SysUserMapper sysUserMapper;


    @RequestMapping("business")
    public ModelAndView business() {
        ModelAndView modelAndView = new ModelAndView();
//        查询为支付的订单
        List<SysOrder> orders = sysOrderMapper.selectOrder();
//        System.out.println(orders);
        modelAndView.addObject("order", orders);
        modelAndView.setViewName("business");
        return modelAndView;
    }

    /*
     * 结束
     * */
    @RequestMapping("/setOrderStaut")
    public String  setOrderStaut(Integer orderId) {
        SysOrder sysOrder = new SysOrder();
        sysOrder.setSysOrderId(orderId);
        sysOrder.setState(1);
        int i = sysOrderMapper.selectOrders(orderId);
        return "redirect:/bus/business";
//        int i = sysOrderMapper.updateById(new SysOrder().setSysOrderId(orderId).setState(1));
//        return "redirect:/bus/business";
    }
    @Autowired
    private DishMapper dishMapper;
    @RequestMapping("/setDishStaut")
    public String  setDishStaut(Integer state,Integer dishId) {
        Dish dish = new Dish();
        dish.setDishId(dishId);
        dish.setState(0);
        int i = dishMapper.selectState(state,dishId);
        return "redirect:/bus/business_menu";}

    @RequestMapping("/business_menu")
    public ModelAndView business_menu() {
        ModelAndView modelAndView = new ModelAndView();
        List<Dish> userDish = dishMapper.allDish();
        modelAndView.addObject("userDishList", userDish);
        modelAndView.setViewName("business_menu");
        return modelAndView;

    }

    @RequestMapping("/business_menu_add")
    public ModelAndView business_menu_add(String categoryName) {
        ModelAndView modelAndView = new ModelAndView();
        List<DishCategory> dishCategory = dishCategoryMapper.selCategoryAll(categoryName);
        modelAndView.addObject("Category",dishCategory);
        modelAndView.setViewName("business_menu_add");
        return  modelAndView;

    }

    @RequestMapping("/business_index")
    public String business_index() {
        return "business_index";
    }

    @Autowired
    private DishCategoryMapper dishCategoryMapper;

    @RequestMapping("addDish")
    @ResponseBody
    public Map addDish(String dishName, String summary, BigDecimal price, Integer caregoryName, String filePath){
        Map<String,Object> map = new HashMap<>();
//        System.out.println("categoryid="+caregoryName);
        Dish dish = new Dish();
        dish.setDishName(dishName);
        dish.setPrice(price);
        dish.setSummary(summary);
        dish.setImg(filePath);
        dish.setState(1);
        DishCategory dishCategory = new DishCategory();
        dishCategory.setDishCategoryId(caregoryName);
        dish.setDishCategory(dishCategory);
        int i = dishMapper.addDish(dish);
        if (i > 0) {
            map.put("res","ok");
            map.put("msg","添加成功");
            return map;
        }
        map.put("res","error");
        map.put("msg","添加失败");
        return map;
    }

    @PostMapping("fileUp")
    @ResponseBody
    public String fileUp(MultipartFile file) throws IOException {
        if (file != null && !file.isEmpty()) {
            String path = new File("").getAbsolutePath();
            String filename = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String type = filename.substring(filename.lastIndexOf("."));
            String s = null;
            file.transferTo(new File(path + "\\file\\" + uuid + type));
            s = uuid + type;
            return s;
        }
        return "0";
    }
}
